import React, { Component } from 'react';
import protobuf from 'protobufjs/minimal';

import root from '../ProtoJs/proto'
class Scada extends Component {
    constructor(props){


        super(props);
        this.state = {
            data: 'jumpData'
        }

        this.handleEvent = this.handleEvent.bind(this);
        console.log(navigator);
        return;

        let data = {name: "ly", passwdMD5: "1234567" };
        let loginData = root.UserInfo.Login.create(data);
        const buffer = root.UserInfo.Login.encode(loginData).finish();
        console.log("Serialized:", buffer);

// 反序列化消息
        const decodedMessage = root.UserInfo.Login.decode(buffer);
        console.log("Deserialized:", decodedMessage);

        //let errmsg = root.UserInfo.Login.verify(loginData);

        //loginData.name = 'ly';
        //loginData.passwordMD5 = '123456';
        console.log('loginData:', loginData.name, loginData.passwdMD5);
        let writer = new protobuf.Writer();
        //return;
        writer.fixed32(3);
        //console.log(root.UserInfo.Login.encode);
        root.UserInfo.Login.encode(loginData, writer);
        let writeData = writer.finish();

        const reader = new protobuf.Reader(writeData);

        let msgTypes = reader.fixed32();
        let mainMsgType = msgTypes >> 16;
        let subMsgType = msgTypes & 0x0f
        let resMsg = root.UserInfo.Login.decode(reader, writeData.size - 4);
        console.log(resMsg, '------');
    }
    handleEvent(){
        //test


        /*
        this.props.history.push("/page2", {
            dotData: {}

        });*/
        console.log(root);
        let data = {name:"ly", passwdMD5:"123456"};

        let loginData = new root.UserInfo.Login(data);
        const  errmsg = root.UserInfo.Login.verify(loginData);
        if (errmsg)
        {
            let a = 0;
            a++;
        }
        //Login.create(properties:Object):Login
        //const root = protobuf.Root.prototype(/* ... */);
        if(!window.WebSocket)
        {
            alert("Websocket not supported in the browser.");
            return;
        }

        let socket = new WebSocket("ws://127.0.0.1:12333");
        socket.onopen = function(event) {
            // 在此处执行初始化操作或发送第一条消息
            //socket.send()

            console.log("opended.....");
        }
        socket.onerror = function (event)
        {
            console.log("onError:", event);
        }

        // function blobToArrayBuffer(blob, callback) {
        //     let reader = new FileReader();
        //     reader.onload = function() {
        //         return callback(this.result);
        //     }
        //     reader.readAsArrayBuffer(blob);
        // }

        socket.onmessage = function(event) {
            let receivedData = event.data;
            console.log("收到消息：" + receivedData);
            if (receivedData instanceof Blob) {
                let fileReader = new FileReader();

                //test writer
                let data = {name:"ly", passwdMD5:"1234567"};
                let loginData = new root.UserInfo.Login(data);
                let writer = new protobuf.Writer();
                writer.fixed32(0);
                loginData.encode(writer);
                let writeData = writer.finish();

                // 设置读取完成后的回调函数
                fileReader.onload = function() {
                    console.log("onLoad...", this.result);
                    // 获取读取的数据，即 ArrayBuffer 对象
                    let dataArrayBuffer = this.result;
                    let test = new Uint8Array(dataArrayBuffer);
                    console.log(111);
                    // 将 ArrayBuffer 传递给 protobuf.Reader.create
                    const reader = new protobuf.Reader(test);

                    let msgTypes = reader.fixed32();
                    let mainMsgType = msgTypes >> 16;
                    let subMsgType = msgTypes & 0x0f
                    let resMsg = root.UserInfo.Login.decode(reader, event.data.size - 4);
                    const decoder = new TextDecoder('utf-8');
                    console.log('name:', decoder.decode(resMsg.name));
                    console.log('password:', decoder.decode(resMsg.passwdMD5));
                    socket.send("123");
                    console.log('send 123');
                    let a = 0;
                    a++;
                    // 在这里使用 reader 对象进行进一步的操作
                    // 例如解析 Protobuf 消息等
                };

                // 开始读取 Blob 对象中的数据
                fileReader.readAsArrayBuffer(receivedData);
            }



        };
        console.log("end....");
        //window.open('/web/alarmview/allAlarmView.html?w=200&h=800');
    }
    render() {
        return (
            <div className="Scada">
                <h2>告警页面</h2>
                <button onClick={this.handleEvent}>点击</button>
            </div>
        );
    }
}
export default Scada;